# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： generate_sort_result
Description :
Author : 'li'
date： 2020/11/4
-------------------------------------------------
Change Activity:
2020/11/4:
-------------------------------------------------
"""
import copy

from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.common_template import \
    CommonTemplate
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.template_process import \
    TemplateProcessor


class SortedResultGenerator:
    def __init__(self, sorted_list):
        """

        :param sorted_list:
        """
        self.sorted_list = copy.deepcopy(sorted_list)
        self.company_number = None
        self.serial_number = None
        self.check_number = None
        self.container_type_number = None
        self.is_valid_container_number = False

    def result_to_json(self):
        return {'company_number': self.company_number, 'serial_number': self.serial_number,
                'check_number': self.check_number, 'container_type_number': self.container_type_number,
                'is_valid_container_number': self.is_valid_container_number}

    def get_direction_result(self):
        """
        get result
        :return:
        """
        format_result_list = self._format_direction_result()
        self._get_direction_result(format_result_list)

    def result_to_string(self):
        res = {'company_number': self.company_number, 'serial_number': self.serial_number,
               'check_number': self.check_number, 'container_type_number': self.container_type_number,
               'is_success': self.is_valid_container_number}
        return res

    def _format_direction_result(self):
        result_list = []
        for row in self.sorted_list:
            res = []
            for item in row:
                res.append(item.recognition_result)
            result_list.append(res)
        return result_list

    def _get_direction_result(self, format_result_list):
        """
        get direction result
        :param format_result_list:
        :return:
        """
        template_processor = TemplateProcessor(format_result_list)
        template = template_processor.get_result()
        if not template.is_valid_container_number:
            tmp_template = CommonTemplate(format_result_list)
            tmp_template.get_result()
            if len(template.company_number) == 0 or len(tmp_template.serial_number) > 0:
                template = tmp_template
        if template is None:
            return
        self.company_number = template.company_number
        self.serial_number = template.serial_number
        self.check_number = template.check_number
        self.container_type_number = template.container_type_number
        self.is_valid_container_number = template.is_valid_container_number
